/**
 * Copyright (c) 2023 myons Inc. All rights reserved.
 */
package cn.zmwh.im.server.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @describe: 请求工具 、
 * @author: dmzmwh 、
 * @time: 2023-04-11 13:18 、
 */
@Slf4j
public class HttpUtil {

    /**
     * If the parameter data was sent in the request body, such as occurs
     * with an HTTP POST request, then reading the body directly via
     *
     * @param request HttpServletRequest
     * @return String
     * @see javax.servlet.ServletRequest#getInputStream or
     * @see javax.servlet.ServletRequest#getReader
     */
    public static String getPostData(HttpServletRequest request) {
        StringBuilder data = new StringBuilder();
        String line;
        BufferedReader reader;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine())) {
                data.append(line);
            }
        } catch (IOException e) {
            return null;
        }
        String s = data.toString();
        return s;
    }

    /**
     * 获取ip地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("chiru-realip");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("RemoteIp");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        } else {
            ip = ip.split(",")[0];
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        if (!StringUtils.isEmpty(ip) && ip.startsWith("100")) {
            Enumeration<String> headerNames = request.getHeaderNames();
            StringBuilder builder = new StringBuilder();
            while (headerNames.hasMoreElements()) {
                String s = headerNames.nextElement();
                builder.append(s);
                builder.append("=");
                builder.append(request.getHeader(s));
            }
            log.info("IP地址100：header:异常：{}", builder.toString());
        }


        return ip;

    }

}
